package com.ill.legou.item.service.impl;

import com.ill.legou.item.dao.BrandDao;
import com.ill.legou.item.po.Brand;
import com.ill.legou.item.po.Category;
import com.ill.legou.item.service.IBrandService;
import com.lxs.legou.core.service.impl.CrudServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class BrandServiceImpl extends CrudServiceImpl<Brand> implements IBrandService {



    @Override
    public List<Category> selectCategoryByBrandId(Long id) {
        return ((BrandDao)getBaseMapper()).selectCategoryByBrand(id);
    }



    //用于控制事务
    @Transactional(readOnly = false)
    @Override
    public boolean saveOrUpdate(Brand entity) {
        boolean result = super.saveOrUpdate(entity);

        //首先去删除原有的这个品牌相关联的分类信息
        ((BrandDao)getBaseMapper()).deleteCategoryByBrand(entity.getId());

        //然后再去更新这个品牌的相关分类信息  ,Brand里边其实已经拥有了这个category的id值
        Long[] roleIds = entity.getCategoryIds();
        if(null != roleIds && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                ((BrandDao)getBaseMapper()).insertCategoryAndBrand(roleId,entity.getId());
            }
        }


        return result;

    }
}
